package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IGradeMapper gradeMapper;
    @Override
    public UserBean login(String userName, String password) {
        return userMapper.login(userName,password);
    }

    @Override
    public IPage<UserBean> cutFindByItem(Integer pageNum, String userRealName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (userRealName != null){
            qw.like("u_realName",userRealName);
        }
        return userMapper.selectPage(new Page<>(pageNum,3),qw);
    }

    @Override
    public void addUser(UserBean userBean, List<Integer> roleIdList) {
        userMapper.insert(userBean);
        userMapper.addUserRole(userBean.getId(), roleIdList);
    }

    @Override
    public void delUser(Integer userId) {
        userMapper.delUserRole(userId);
        userMapper.deleteById(userId);
    }

    @Override
    public UserBean findRole(Integer userId) {
        return userMapper.findRole(userId);
    }

    @Override
    public void updatePassword(Integer userId, String newPassword) {
        UserBean userBean = userMapper.selectById(userId);
        userBean.setPassword(newPassword);
        userMapper.updateById(userBean);
    }
}
